<template>
  <el-cascader
    value="value"
    clearable
    :props="{value:'id',label:'name'}"
    :options="options"
    @change="handleChange"
  />
</template>

<script>
import { companyDepartmentRequest } from '@/api/department'
import { transformListToTree } from '@/utils/index.js'
export default {
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      options: []
    }
  },
  created() {
    this.companyDepartment()
  },
  methods: {
    async companyDepartment() {
      const res = await companyDepartmentRequest()
      // 利用递归来遍历树
      this.options = transformListToTree(res.data, 0)
    },
    handleChange(val) {
      // 我们一般传值都是传最后一个，所以会进行判断
      if (val.length > 0) {
        this.$emit('input', val[val.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>

<style>

</style>
